package so.sao.code.service.domain.handler.query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import so.sao.code.service.domain.entity.EnterpriseDomain;
import so.sao.code.service.domain.event.query.GetEnterpriseSubDomainEvent;
import so.sao.code.service.repository.EnterpriseDomainDao;

import java.util.Optional;

/**
 * Created by Administrator on 2017/10/31.
 */
@Component
public class GetEnterpriseSubDomainHandler {

    @Value("${sao.code.key.default-sub-domain}")
    private String defaultSubDomain;
    @Autowired
    EnterpriseDomainDao enterpriseDomainDao;

    @EventListener
    public void getSubDomain(GetEnterpriseSubDomainEvent event) {
        Optional<EnterpriseDomain> enterpriseDomain = Optional.ofNullable(enterpriseDomainDao.findSubByEseId(event.getEseId()));
        event.setSubDomain(enterpriseDomain.isPresent() ? enterpriseDomain.get().getSubDomain() : defaultSubDomain);
    }
}
